package com.bwie.utils;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.bwie.app.MyApplication;

/**
 * Created by PC on 2016/10/31.
 */
public class CommonUtils {
    public static final String TAG="TopNews";
    private static SharedPreferences sharedPreferences;

    public static View inflate(int layoutId)
    {
        View view=View.inflate(MyApplication.getContext(),layoutId,null);
        return view;
    }
    public static int dip2px(int dip)
    {
        float den=MyApplication.getContext().getResources().getDisplayMetrics().density;
        int px= (int) (dip*den+0.5f);
        return px;
    }
    public static int px2dip(int px)
    {
        float den=MyApplication.getContext().getResources().getDisplayMetrics().density;
        int dip= (int) (px/den+0.5f);
        return dip;
    }
    public static String getString(int stringId)
    {
        return MyApplication.getContext().getResources().getString(stringId);
    }
    public static Drawable getDrawable(int did)
    {
        return MyApplication.getContext().getResources().getDrawable(did);
    }
    public static void saveSp(String flag,String str)
    {
        if(sharedPreferences==null)
        {
            sharedPreferences=MyApplication.getContext().getSharedPreferences(TAG,MyApplication.getContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(flag, str);
        edit.commit();
    }
    public static void runOnUIThread(Runnable runable) {
        if(android.os.Process.myTid()==MyApplication.getMyTid()){
            runable.run();
        }else{
            MyApplication.getHandler().post(runable);
        }

    }
    public static void executeRunnalbe(Runnable runable){
        MyApplication.getExecutorService().execute(runable);
    }
}
